package com.example.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.PmCompeteManager;
import com.example.demo.entity.PmEmployee;
import com.example.demo.mapper.PmCompeteManagerMapper;
import com.example.demo.mapper.PmEmployeeMapper;
import com.example.demo.service.impl.PmCompeteManagerServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("/compete")
public class PmCompeteManagerController extends BaseController {
    @Resource
    PmEmployeeMapper pmEmployeeMapper;
    @Resource
    PmCompeteManagerMapper pmCompeteManagerMapper;
    @Autowired
    PmCompeteManagerServiceImpl pmCompeteManagerService;

    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @PostMapping
    public synchronized Result<?> saveOrUpdate(@RequestBody PmCompeteManager pmCompeteManager) {
        PmEmployee pmEmployee = pmEmployeeMapper.selectById(pmCompeteManager.getEmployeeId());
        pmCompeteManager.setModifyTime(formatter.format(new Date()));
        if (StrUtil.isEmpty(pmEmployee.getSex()) || StrUtil.isEmpty(pmEmployee.getBirth())){
            return Result.error("401", "请先完善该人员的性别与生日信息");
        }
        pmCompeteManager.setSex(pmEmployee.getSex());
        pmCompeteManager.setBirth(pmEmployee.getBirth().substring(0, 7));
        pmCompeteManager.setCompany(pmEmployee.getCompany());
        pmCompeteManager.setOldDepartment(pmEmployee.getDepartment());
        pmCompeteManager.setOldPosition(pmEmployee.getPosition());
        pmCompeteManagerService.saveOrUpdate(pmCompeteManager);

        if (pmCompeteManager.getType().equals("内部流转")){
            pmEmployee.setRecruitmentChannels("竞争上岗");
        } else {
            pmEmployee.setRecruitmentChannels(pmCompeteManager.getType());
        }
        pmEmployee.setDepartment(pmCompeteManager.getNowDepartment());
        pmEmployee.setPosition(pmCompeteManager.getNowPosition());
        pmEmployee.setPositionStatus(pmCompeteManager.getPositionStatus());
        pmEmployeeMapper.updateById(pmEmployee);

        return Result.success();
    }

    @GetMapping("/{employeeId}")
    public synchronized Result<?> get(@PathVariable Long employeeId) {
        return Result.success(pmCompeteManagerMapper.get(employeeId));
    }

    @GetMapping("/list")
    public synchronized Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                                           @RequestParam(defaultValue = "10") Integer pageSize,
                                           @RequestParam(defaultValue = "") String search,
                                           @RequestParam(defaultValue = "") Long employeeId,
                                           @RequestParam(defaultValue = "") String company) {
        LambdaQueryWrapper<PmCompeteManager> wrapper = Wrappers.lambdaQuery();
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(PmCompeteManager::getName, search);
        }
        if (StrUtil.isNotBlank(company) && !company.equals("会博集团及下属子公司")) {
            wrapper.eq(PmCompeteManager::getCompany, company);
        }
        if (employeeId != null) {
            wrapper.eq(PmCompeteManager::getEmployeeId, employeeId);
        }
        Page<PmCompeteManager> pmEmployeePage = pmCompeteManagerMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(pmEmployeePage);
    }
}
